package edu.whu.demo.controller;


import edu.whu.demo.domain.Commdity_SuppliersDTO;
import edu.whu.demo.domain.Commodity;
import edu.whu.demo.service.impl.CommodityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zxj
 * @since 2023-10-18
 */
@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    CommodityServiceImpl commodityService;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Commodity> addSupp(Commodity Commodity){
        Commodity Commodity1 = commodityService.addCommdity(Commodity);
        return ResponseEntity.ok(Commodity1);
    }

    @PostMapping("/del")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Void> deleteSupp(int id){
        commodityService.delCommdityById(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('commodity/change')")
    public ResponseEntity<Void> updateSupp(Commodity Commodity){
        commodityService.updateCommdityById(Commodity);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/find")
    @PreAuthorize("hasAuthority('commodity/findAllSuppliers')")
    public ResponseEntity<Commodity> findSuppById(int id){
        Commodity Commodity = commodityService.findCommdityById(id);
        return ResponseEntity.ok(Commodity);
    }

    @PostMapping("/findAllSuppliers/{id}")
    @PreAuthorize("hasAuthority('commodity/findAllSuppliers')")
    public ResponseEntity<Commdity_SuppliersDTO> findAllSuppliers(@PathVariable int id){
        Commdity_SuppliersDTO commditySuppliersDTO = commodityService.getCommdityWithSuppliers(id);
        return ResponseEntity.ok(commditySuppliersDTO);
    }
}

